package com.rubiksillusions;

public class PositionThreeCornersOfFirstLayerPatternCreator {
	public static void main(String[] args) {
		String finalStrings[] = {
			"....w..........o......y...y....................w.o....",
			"....w..........o......y...w....................y.o....",
			"....w..........y......y...w....................o.o....",
			"....w..........y......y...o....................w.o....",
			"....w..........w......y...y....................o.o....",
			"....w..........w......y...o....................y.o...."
		};
		
		String initStrings[] = {
//			"....w............o....y......y...................o...w",
//			"....w............o....y......w...................o...y",
//			"....w............y....y......w...................o...o",
//			"....w............y....y......o...................o...w",
//			"....w............w....y......y...................o...o",
//			"....w............w....y......o...................o...y",
//			
//			"....w...o.............y....y.....................o.w..",
//			"....w...o.............y....w.....................o.y..",
//			"....w...y.............y....w.....................o.o..",
//			"....w...y.............y....o.....................o.w..",
//			"....w...w.............y....y.....................o.o..",
//			"....w...w.............y....o.....................o.y..",
//			
//			"..o.w.................y.y....................w...o....",
//			"..o.w.................y.w....................y...o....",
//			"..y.w.................y.w....................o...o....",
//			"..y.w.................y.o....................w...o....",
//			"..w.w.................y.y....................o...o....",
//			"..w.w.................y.o....................y...o....",
//			
//			"....w.o...............y..........y....w..........o....",
//			"....w.o...............y..........w....y..........o....",
//			"....w.y...............y..........w....o..........o....",
//			"....w.y...............y..........o....w..........o....",
//			"....w.w...............y..........y....o..........o....",
//			"....w.w...............y..........o....y..........o....",
//			
//			"....w......o..........y............y........w....o....",
//			"....w......o..........y............w........y....o....",
//			"....w......y..........y............w........o....o....",
//			"....w......y..........y............o........w....o....",
//			"....w......w..........y............y........o....o....",
//			"....w......w..........y............o........y....o....",
//			
//			"....w....o..........y.y...................w......o....",
//			"....w....o..........w.y...................y......o....",
//			"....w....y..........w.y...................o......o....",
//			"....w....y..........o.y...................w......o....",
//			"....w....w..........y.y...................o......o....",
//			"....w....w..........o.y...................y......o....",
//			
//			"o...w.............y...y.............w............o....",
//			"o...w.............w...y.............y............o....",
//			"y...w.............w...y.............o............o....",
//			"y...w.............o...y.............w............o....",
//			"w...w.............y...y.............o............o....",
//			"w...w.............o...y.............y............o....",
			
			"....w...o.............o....y.....................y.w..",
			"....w...o.............o....w.....................y.y..",
			"....w...y.............o....w.....................y.o..",
			"....w...y.............o....o.....................y.w..",
			"....w...w.............o....y.....................y.o..",
			"....w...w.............o....o.....................y.y.."
		};
		
		String[] masks = {
//			"ww.w.wwww.........g..g.....b..b..b..ooo.........r..r..", // Cubelet 6, 0, 2 (1, 3, 5, 7)
//			"ww.w.www..........g..g........b..b..ooo.........r.....", // Cubelet 6, 0 (1, 3, 5, 7)
//			".w.w.wwww............g.....b..b..b...oo.........r..r..", // Cubelet 2, 0 (1, 3, 5, 7)
//			"ww.w.w.ww.........g..g.....b..b.....bo..........r..r..", // Cubelet 6, 2 (1, 3, 5, 7)
//			"ww.w.w.w..........g..g........b.....oo..........r.....", // Cubelet 6 (1, 3, 5, 7)
//			".w.w.www.............g........b..b...oo.........r.....", // Cubelet 0 (1, 3, 5, 7)
//			".w.w.w.ww............g.....b..b......o..........r..r..", // Cubelet 2 (1, 3, 5, 7)
			".w.w.w.w.............g........b......o..........r....." // Nessun Cubelet (1, 3, 5, 7)
		};
		
		for (int k = 0; k < masks.length; k++) {
			for (int j = 0; j < initStrings.length; j++) {
				String output = getJavaForPattern(
					"",
					initStrings[j],
					finalStrings,
					masks[k]
				);
				if (output != null && !output.equals("")) System.out.println(output);
			}
			System.out.println("\n\n");
		}
	}
	
	private static String getJavaForPattern(String name, String initString, String[] finalStrings, String mask) {
		String javaString = "";
		String moves = getMovesForPattern(initString, finalStrings, mask);
		if (moves == null) return "";
		if (moves.equals("")) return "";
		javaString += "if (!executed) executed = rubiksCube.executeStatusMovesList(" +
			"\"" + initString + "\", " + "\"" +
			moves + "\", " + "true);";
		return javaString;
	}
	
	private static String getMovesForPattern(String initString, String[] finalStrings, String mask) {
		int MAX_MOVES = 4;
		String tempInitString = "";
		String[] tempFinalStrings = new String[finalStrings.length];
		for (int i = 0; i < tempFinalStrings.length; i++) {
			tempFinalStrings[i] = "";
		}
		
		
		for (int i = 0; i < mask.length(); i++) {
			if (mask.charAt(i) != '.') {
				if (initString.charAt(i) != '.') return null;
				tempInitString += mask.charAt(i);
				for (int j = 0; j < finalStrings.length; j++) {
					tempFinalStrings[j] += mask.charAt(i);
				}
				
			} else {
				tempInitString += initString.charAt(i);
				for (int j = 0; j < finalStrings.length; j++) {
					tempFinalStrings[j] += finalStrings[j].charAt(i);
				}
			}
		}
		int i = 0, movesCount = 0;
		while (movesCount <= MAX_MOVES) {
			int temp = i;
			String out = "";
			out += getMoveByNumber(temp % 18) + " ";
			while (temp / 18 != 0) {
				temp = temp / 18;
				out += getMoveByNumber(temp % 18) + " ";
			}
			
			String[] splitted = out.split(" ");
			movesCount = splitted.length;
			if (movesCount > MAX_MOVES) break;
			
			RubiksCube initCube = new RubiksCube(tempInitString);
			initCube.executeMovesList(out, true);
			
			String exCube = initCube.getFaceletsString();
			//if (i % 100 == 0) System.out.println(i);
			//System.out.println(exCube + " " + out);
			i++;
			
			for (int j = 0; j < tempFinalStrings.length; j++) {
				//System.out.println("    " + tempFinalStrings[j]);
				if (exCube.equals(tempFinalStrings[j])) {
					return out;
				}
			}
		}
		return "";
	}
	
	private static String getMoveByNumber(int number) {
		switch (number) {
			case 0: return "R";
			case 1: return "L";
			case 2: return "U";
			case 3: return "D";
			case 4: return "F";
			case 5: return "B";
			case 6: return "R'";
			case 7: return "L'";
			case 8: return "U'";
			case 9: return "D'";
			case 10: return "F'";
			case 11: return "B'";
			case 12: return "R2";
			case 13: return "L2";
			case 14: return "U2";
			case 15: return "D2";
			case 16: return "F2";
			case 17: return "B2";
		}
		return "";
	}
}
